package switchviz.graphic.gauge.bargraph.scale;

import org.jdom.Element;

import switchviz.graphic.IGraphicsPanel;
import switchviz.gui.component.JColorPanel;
import switchviz.gui.component.JMaskedTextField;


public class BarGraphScalePanel extends IGraphicsPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -840991172609138648L;
	private Element _root = null;
	private BarGraphScalePersistence _nodehandler = null;
	private BarGraphScaleDataModel _scalemodel = null;
	private JMaskedTextField _txtnumgridline = null;

	private JColorPanel _clgridcolor = null;
	private JColorPanel _clcoordinatecolor = null;
	
	public Element getElement()
	{
		getScaleModel().setNumgridlines(Integer.valueOf(_txtnumgridline.getText()));
		getScaleModel().setGridcolor(_clgridcolor.getColor());
		getScaleModel().setCoordinatecolor(_clcoordinatecolor.getColor());
		_root = _nodehandler.setDataModel(getScaleModel());
		return _root;
	}
	
	public BarGraphScalePanel(Element root)
	{
		super();
		_root = root;
		initialize();
		setScaleModel(getNodeHandler().getDataModelFromElement());
	}
	
	private void initialize()
	{
		this.setName("BarGraph Scale");

		_txtnumgridline = new JMaskedTextField("Number of Gridlines: ", JMaskedTextField.INTNUM);
		_clgridcolor = new JColorPanel("Grid Color: ");
		_clcoordinatecolor = new JColorPanel("Coordinate Color: ");
		this.add(_txtnumgridline);
		this.add(_clgridcolor);
		this.add(_clcoordinatecolor);
	}
	
	private BarGraphScalePersistence getNodeHandler()
	{
		if(_nodehandler == null)
			_nodehandler = new BarGraphScalePersistence(_root);
		return _nodehandler;
	}
	
	private BarGraphScaleDataModel getScaleModel()
	{
		if(_scalemodel == null)
			_scalemodel = new BarGraphScaleDataModel();
		return _scalemodel;
	}
	
	private void setScaleModel(BarGraphScaleDataModel scalemodel)
	{
		_scalemodel = scalemodel;
		_txtnumgridline.setText(String.valueOf(_scalemodel.getNumgridlines()));
		_clgridcolor.setColor(_scalemodel.getGridcolor());
		_clcoordinatecolor.setColor(_scalemodel.getCoordinatecolor());
	}

	@Override
	public void setElement(Element node) {}
}
